package cn.telling.tianlianproduct.productonoffSale.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.telling.tianlianproduct.freightrules.service.IFreightRulesService;
import cn.telling.tianlianproduct.freightrules.vo.ProductFreightRulesAreaVo;
import cn.telling.tianlianproduct.freightrules.vo.ProductFreightRulesVo;
import cn.telling.tianlianproduct.productonoffSale.dao.ProductOnOffSaleMapper;
import cn.telling.tianlianproduct.productonoffSale.service.IOnOffSale;
import cn.telling.tianlianproduct.productonoffSale.vo.ShopProductAreaVo;
import cn.telling.tianlianproduct.productonoffSale.vo.saleListConditionVo;
import cn.telling.tianlianproduct.sellerbaseprice.dao.SellerBasePriceMapper;
import cn.telling.tianlianproduct.sellerbaseprice.vo.SellerBasePriceVo;


/** 
* @ClassName: ProductOnOffSale 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author luoqingm tianlian.com 
* @date 2015年9月24日 下午8:11:11 
*  
*/

@Service("productOnOffSale")
public class ProductOnOffSale implements IOnOffSale {
	
	@Autowired
	private ProductOnOffSaleMapper  productOnOffSaleMapper;
	
    @Autowired
    @Qualifier("sellerBasePriceMapper")
    private SellerBasePriceMapper sellerBasePriceMapper;
    
	@Autowired
    private IFreightRulesService freightRulesService;
	
	@Override
	public HashMap<String, List<ShopProductAreaVo>> getSaleListMap(	List<ShopProductAreaVo> shopProductAreaVoList) {
		List<ShopProductAreaVo> newShopProductAreaVoList = new LinkedList<ShopProductAreaVo>();
		HashMap<String, List<ShopProductAreaVo>> shopProductAreaVoListMap = new LinkedHashMap<String, List<ShopProductAreaVo>>();
		
	if(shopProductAreaVoList != null && shopProductAreaVoList.size() > 0){
			for(int i = 0; i < shopProductAreaVoList.size(); i++){
				ShopProductAreaVo shopProductAreaVo = shopProductAreaVoList.get(i);
				if("1".equals(shopProductAreaVo.getIsShare())){
					if(shopProductAreaVoListMap.get(shopProductAreaVo.getShareCode()) != null){
						shopProductAreaVoListMap.get(shopProductAreaVo.getShareCode()).add(shopProductAreaVo);
					}else {
						List<ShopProductAreaVo> list = new ArrayList<ShopProductAreaVo>();
						list.add(shopProductAreaVo);
						
						shopProductAreaVoListMap.put(shopProductAreaVo.getShareCode(), list);
					}
				}else {
					newShopProductAreaVoList.add(shopProductAreaVo);
				}
			}
		}		
		if(newShopProductAreaVoList != null && newShopProductAreaVoList.size() > 0){
			shopProductAreaVoListMap.put("0", newShopProductAreaVoList);
		}
		return shopProductAreaVoListMap;		
	}
	
	@Override
	public List<ShopProductAreaVo> getSaleListByCondition(saleListConditionVo conditionVo) {
		// TODO Auto-generated method stub
		 List<ShopProductAreaVo> productVoList= productOnOffSaleMapper.getSaleListByCondition(conditionVo);
		 /**取出数据，判断不可销售供应商类型是否为通信市场，如果是的 ，那么产品不能上架，不能被修改*/
		 if(productVoList!=null){
		 for(ShopProductAreaVo vo : productVoList){
			 
			 if(vo.getNosupplytype()!=null &&(vo.getNosupplytype().indexOf("8")>0 || vo.getNosupplytype().indexOf("08")>0)){
				 
				 vo.setNosupplytype("1");
			 } 
		   }
		}
			/**运费规则代码**/
	    for(ShopProductAreaVo vo : productVoList){
			   StringBuffer rulesb=new 	StringBuffer();
			   List<ProductFreightRulesVo> ProductFreightRules =freightRulesService.getProductFreightRulesBySlyArea0Id(vo.getSupplyAreaId());
			   if(ProductFreightRules!=null){
				   for(ProductFreightRulesVo rulesVo:ProductFreightRules){
					   if("2".equals( rulesVo.getType())){
						   
						   if(rulesb.length()>0){
							   rulesb.append(",<br>");
						   }
						   rulesb.append("卖家包邮：");
						   String areaname="";
						   List<ProductFreightRulesAreaVo> freightList=  rulesVo.getAreaFreightList();
						   for(ProductFreightRulesAreaVo productVo:freightList ){
							   areaname += productVo.getAreaname()+"、";
						   }
						   areaname=areaname.substring(0, areaname.length()-1);
						   rulesb.append(areaname);
					   }
					   if("3".equals( rulesVo.getType())){
						   if(rulesb.length()>0){
							   rulesb.append(",<br>");
						   }
						   rulesb.append(rulesVo.getFee()+"元以上满"+rulesVo.getNum()+"台包邮,"+ "否则收取"+rulesVo.getRoleprice()+"元：" );	
						   String areaname="";
						   List<ProductFreightRulesAreaVo> freightList=  rulesVo.getAreaFreightList();
						   for(ProductFreightRulesAreaVo productVo:freightList ){
							   areaname += productVo.getAreaname()+"、";
						   }
						   areaname=areaname.substring(0, areaname.length()-1);
						   rulesb.append(areaname);
					   }
                      if("5".equals( rulesVo.getType())){
                          if(rulesb.length()>0){
							   rulesb.append(",<br>");
						  }
					      rulesb.append("每笔订单满"+rulesVo.getFee()+"元包邮"+"否则收取"+rulesVo.getFee()+"元：");	
					      String areaname="";
                 	      List<ProductFreightRulesAreaVo> freightList=  rulesVo.getAreaFreightList();
						  for(ProductFreightRulesAreaVo productVo:freightList ){
							   areaname += productVo.getAreaname()+"、";
						  }
						  areaname=areaname.substring(0, areaname.length()-1);
						  rulesb.append(areaname);
					   }					   
				   }
			   }
			   vo.setFeightRules(rulesb.toString());
			}
		 
		 return productVoList;
	}

	@Override
	public int onSaleOff(List<ShopProductAreaVo> shopProductAreaVoList,String saleStatus) {
		// TODO Auto-generated method stub
	
	   return	productOnOffSaleMapper.onSaleOff(shopProductAreaVoList,saleStatus);
		
	}

	@Override
	@Transactional(value = "txManager", isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })
	public int modify( List<ShopProductAreaVo> shopProductAreaVoList, List<ProductFreightRulesVo> ProductFreightRulesVoList,String userId) {
     
		if(ProductFreightRulesVoList!=null&&ProductFreightRulesVoList.size()>0){
			List<ProductFreightRulesVo> list=new ArrayList<ProductFreightRulesVo>();
			for(int i=0;i<ProductFreightRulesVoList.size();i++)	{	
			   list.add( ProductFreightRulesVoList.get(i) );
			   if("1".equals( ProductFreightRulesVoList.get(i).getCheckflag()) ){
		          this.freightRulesService.insertShopFreightRules(ProductFreightRulesVoList,userId );
			   }
			   if("0".equals( ProductFreightRulesVoList.get(i).getCheckflag())){
			          this.freightRulesService.deleteShopFreightRules(ProductFreightRulesVoList);
			     }
			}
		}		
		int num=0;int count=0;
		if(shopProductAreaVoList.size()>0){
		for(ShopProductAreaVo vo :shopProductAreaVoList){
			  count= productOnOffSaleMapper.updatesupplyArea0(vo);				
		} 
		if(count>0){
			for(ShopProductAreaVo shopProductAreavo:shopProductAreaVoList){
				
				SellerBasePriceVo basevo=new SellerBasePriceVo();			
				basevo.setBasePrice(shopProductAreavo.getPriceRetailOnline());
				basevo.setProductId(shopProductAreavo.getProductId() );
				basevo.setSupplyId(shopProductAreavo.getSupplyId() );
				basevo.setUserId( BigDecimal.valueOf(shopProductAreavo.getAddUser()) );
				num =sellerBasePriceMapper.updateSellerBasePrice(basevo);						
		  }	
	    }
	   }
	 return num;
  }
	

}





